<template>
  <div class="home-page">
    <h1>{{data}} - {{test.total}} - {{test.doubleTotal}}</h1>
    <button @click="updateStore">Update</button>
    <section class="py-5 text-center container">
      <div class="row py-lg-5">
        <div class="col-lg-6 col-md-8 mx-auto">
          <img src="../assets/callout.svg" alt="callout" class="w-50"/>
          <h2 class="font-weight-light">随心写作，自由表达</h2>
          <p>
            <a href="#" class="btn btn-primary my-2">开始写文章</a>
          </p>
        </div>
      </div>
    </section>
    <uploader action="/upload" :beforeUpload="beforeUpload" @file-uploaded="onFileUploaded">
      <!-- <h2>点击上传</h2> -->
      <!-- <template #loading>
        <div class="spinner-border" role="status">
          <span class="sr-only">loading</span>
        </div>
      </template> -->
      <template #uploaded="dataProps">
        <img :src="dataProps.uploadedData.data.url" width="500" />
      </template>
    </uploader>
    <h4 class="font-weight-bold text-center">发现精彩</h4>
    <column-list :list="list"></column-list>
    <button
      class="btn btn-outline-primary mt-2 mb-5 mx-auto btn-block w-25 d-block"
       @click="loadMorePage" v-if="!isLastPage"
    >
      加载更多
    </button>
  </div>
</template>

<script lang="ts">
import { defineComponent,computed, onMounted } from 'vue';
import { useStore } from 'vuex';
import { storeToRefs } from 'pinia'
import { GlobalDataProps,ResponseType,ImageProps } from '../store'
import { useTest2Store } from '../store/test'
import useLoadMore from '../hooks/useLoadMore';
import ColumnList from '../components/ColumnList.vue';
import Uploader from '../components/Uploader.vue'

export default defineComponent({
  name: 'Home',
  components: {
    ColumnList,
    Uploader
  },
  setup() {
    const store = useStore<GlobalDataProps>();
    const testStore = useTest2Store()
    
    const { data } = storeToRefs(testStore)
    const total = computed(() => store.state.columns.total );
    const currentPage = computed(() => store.state.columns.currentPage );
    // const selectedColumn = computed(() => testStore.getDataById('2345'))
    onMounted(() => {
      store.dispatch('fetchColumns',{ pageSize:3})
    })
    const updateStore = async() =>{
      // testStore.data.push({_id:'1234',title:'hello there',description:'hello desc'})
      // testStore.total = 10,
      // testStore.$patch({
      //   data:[],
      //   total:20
      // })
      // testStore.$patch((state) =>{
      //   state.data.push({_id:'2345',title:'hello there',description:'hello desc'}),
      //   state.total = 30
      // })
      // testStore.increaseTotal()
      //testStore.$reset()
      const data = await testStore.fetchColumns()
    }
    testStore.$subscribe((mutation, state) => {
      console.log('sub',mutation, state);
    })

    // const list = computed(() => store.state.columns)
    const list = computed(() => store.getters.getColumns)
    const { loadMorePage, isLastPage } = useLoadMore('fetchColumns',total,{pageSize:3, currentPage:(currentPage.value ? currentPage.value + 1 :2)})
    return {
      list,
      loadMorePage,
      isLastPage,
      test: testStore,
      updateStore,
      data,
      // getDataById
    }
  }
})
</script>